#pragma once
#include "util/Singleton.h"
#include "util/threads.h"
#include <unordered_map>
#include <memory>
#include <mutex>
class CServerNode;

class CNetIOManager;

class CTCPServer
        : public qcutil::Singleton<CTCPServer>
{
public:
    CTCPServer();
    ~CTCPServer();
public:
    void start();
    void stop();
    void epoll_update(int epoll_opcode, int fd, epoll_event* ev);
private:
    static void *IOThreadFunction(void* lp);
    void IOThreadImpl();
    void close_and_remove_from_list(int fd);
private:
    std::recursive_mutex m_mutex_epoll_fd;
    int m_fd_epoll;
    int m_fd_listen; //监听的socket。
    qcutil::CThreadBase m_IOThread;
    bool m_quit_flag;
    CNetIOManager* m_io_mgr;
    
    std::unordered_map<int, std::shared_ptr<CServerNode>> m_connections;
    std::recursive_mutex m_mutex_conn;
};
